package it.free.maya;

import java.net.InetAddress;
import java.net.URL;

import org.apache.log4j.Logger;

/**
 * Keep a solr shard host configuration
 * @author Stefano Falconetti
 */
public class SolrCore {

	private String myShardUrl = null;
	private String myQueryUrl = null;
	private String myPingUrl = null;
	private SolrShardType hostType = null;
	private boolean localAddress = false;
	
	private static Logger logger = Logger.getLogger(SolrCore.class);
	
	/**
	 * Class builder
	 * @param url is the url read from the config. Must have the protocol, valid URL format
	 * @param thisHostType can be primary or backup
	 * @throws Exception
	 */
	public SolrCore(String url, SolrShardType thisHostType, String pingUrl) throws Exception {
	
		//Validate the url
		URL urlAsURL = new URL(url);
		this.hostType =  thisHostType;
		this.myQueryUrl = url;
		this.myShardUrl = urlAsURL.getHost() + ":" + urlAsURL.getPort() + urlAsURL.getPath();
		this.myPingUrl = pingUrl;
		
		InetAddress[] inetAddresses = InetAddress.getAllByName(urlAsURL.getHost());
		
		for(InetAddress anInetAddress:inetAddresses){
			
			logger.info("Address [" + anInetAddress + "] is isAnyLocalAddress: " + anInetAddress.isAnyLocalAddress() + ", isLinkLocalAddress: " + anInetAddress.isLinkLocalAddress() + ", isSiteLocalAddress: " + anInetAddress.isSiteLocalAddress() + ", isLoopbackAddress: " + anInetAddress.isLoopbackAddress());
			
			if(anInetAddress.isAnyLocalAddress() || 
			   anInetAddress.isLinkLocalAddress() ||
			   anInetAddress.isSiteLocalAddress() ||
			   anInetAddress.isLoopbackAddress()){
			   this.localAddress = true;
			   break;
			}
		}
	}

	/**
	 * @return the hostType
	 */
	public SolrShardType getHostType() {
		return this.hostType;
	}
	
	/**
	 * Render as a string to be used by the shard query
	 */
	public String toShardURL(){
		return this.myShardUrl;
	}

	/**
	 * Render as a string to be used by a standard query (beginning with http)
	 */
	public String toQueryURL(){
		return this.myQueryUrl;
	}
	
	/**
	 * @return the pingPath
	 */
	public String getPingUrl() {
		return this.myPingUrl;
	}

	/**
	 * Give a representation of this object
	 * @return info about this object as a print friendly string
	 */
	public String toString(){
		
		StringBuilder objectRepresentation = new StringBuilder();
		
		objectRepresentation.append("-------------------------------");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("host type: [" + this.hostType + "]");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("is local address: [" + this.localAddress + "]");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("query url: [" + this.myQueryUrl + "]");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("shard query url: [" + this.myShardUrl + "]");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("ping url: [" + this.myPingUrl + "]");
		objectRepresentation.append(System.getProperty("line.separator"));
		objectRepresentation.append("-------------------------------");
		
		return objectRepresentation.toString();
		
	}

	/**
	 * @return the localAddress
	 */
	public boolean isLocalAddress() {
		return this.localAddress;
	}
	
}
